﻿using System;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents a multi-line editable text control.
    /// </summary>
    [Serializable]
    public class TextArea : Element {
        /// <summary>
        /// Represents the possible choies for wrapping this control.
        /// </summary>
        public enum WrapChoices {
            /// <summary>
            /// Automatically inserts linebreaks when a line of input expands
            /// past the bounds of the control.
            /// </summary>
            Hard,
            /// <summary>
            /// Does not insert any additional linebreaks.
            /// </summary>
            Soft
        }

        private bool _autofocus;
        private int? _cols;
        private bool _disabled;
        private Form _form;
        private int? _maxlength;
        private string _name;
        private string _placeholder;
        private bool _readonly;
        private bool _required;
        private int? _rows;
        private string _selectionDirection;
        private int? _selectionEnd;
        private int? _selectionStart;
        private WrapChoices? _wrap;

        /// <summary>
        /// Determines whether this control has input focus when the page is loaded.
        /// </summary>
        public bool AutoFocus {
            get { return _autofocus; }
            set { _autofocus = value; }
        }

        /// <summary>
        /// Determines the number of columns in this control.
        /// </summary>
        public int? Cols {
            get { return _cols; }
            set { _cols = value; }
        }

        /// <summary>
        /// Determines whether or not this control is disabled by default.
        /// </summary>
        public bool Disabled {
            get { return _disabled; }
            set { _disabled = value; }
        }

        /// <summary>
        /// Determines the parent form of this control.
        /// </summary>
        public Form Form {
            get { return _form; }
            set { _form = value; }
        }

        /// <summary>
        /// Determines the maximum number of characters that the user can
        /// enter.
        /// </summary>
        public int? MaxLength {
            get { return _maxlength; }
            set { _maxlength = value; }
        }

        /// <summary>
        /// Determines the name of this control.
        /// </summary>
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Provides a hint to the user as to what can be contained in this
        /// control.
        /// </summary>
        public string PlaceHolder {
            get { return _placeholder; }
            set { _placeholder = value; }
        }

        /// <summary>
        /// Determines whether or not this control is read-only.
        /// </summary>
        public bool ReadOnly {
            get { return _readonly; }
            set { _readonly = value; }
        }

        /// <summary>
        /// Determines whether or not input in this control is required for the
        /// form to pass validation.
        /// </summary>
        public bool Required {
            get { return _required; }
            set { _required = value; }
        }

        /// <summary>
        /// Determines the number of rows in this control.
        /// </summary>
        public int? Rows {
            get { return _rows; }
            set { _rows = value; }
        }

        /// <summary>
        /// Determines the order in which items were entered into this control.
        /// </summary>
        public string SelectionDirection {
            get { return _selectionDirection; }
            set { _selectionDirection = value; }
        }

        /// <summary>
        /// Determines the index of the last character in the current
        /// selection.
        /// </summary>
        public int? SelectionEnd {
            get { return _selectionEnd; }
            set { _selectionEnd = value; }
        }

        /// <summary>
        /// Determines the index of the first character in the current
        /// selection.
        /// </summary>
        public int? SelectionStart {
            get { return _selectionStart; }
            set { _selectionStart = value; }
        }

        /// <summary>
        /// Determines how the control wraps text.
        /// </summary>
        public WrapChoices? Wrap {
            get { return _wrap; }
            set { _wrap = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _autofocus, "autofocus");
            AttributeWriter.Write(sb, _cols, "cols");
            AttributeWriter.Write(sb, _disabled, "disabled");
            if (_form != null) {
                AttributeWriter.Write(sb, _form.ID, "form");
            }
            AttributeWriter.Write(sb, _maxlength, "maxlength");
            AttributeWriter.Write(sb, _name, "name");
            AttributeWriter.Write(sb, _placeholder, "placeholder");
            AttributeWriter.Write(sb, _readonly, "readonly");
            AttributeWriter.Write(sb, _required, "required");
            AttributeWriter.Write(sb, _rows, "rows");
            AttributeWriter.Write(sb, _selectionDirection,
                "selectiondirection");
            AttributeWriter.Write(sb, _selectionEnd, "selectionend");
            AttributeWriter.Write(sb, _selectionStart, "selectionstart");
            AttributeWriter.Write(sb, _wrap, "wrap", true);
        }

    }
}
